package com.sikaryofficial.backend.controller.information;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.sikaryofficial.backend.annotation.ReloadActionDataCache;
import com.sikaryofficial.backend.annotation.Repeat;
import com.sikaryofficial.backend.constant.ArticleContentTypeEnum;
import com.sikaryofficial.backend.domain.dto.SysDictDataDTO;
import com.sikaryofficial.backend.domain.dto.req.information.ArticleAssociateListReq;
import com.sikaryofficial.backend.domain.dto.req.information.ArticleReq;
import com.sikaryofficial.backend.manager.DataRemoveManager;
import com.sikaryofficial.backend.manager.information.ArticleFrontEndManager;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.core.utils.PageUtil;
import com.sikaryofficial.common.core.web.controller.BaseController;
import com.sikaryofficial.common.core.web.domain.PageResult;
import com.sikaryofficial.common.security.utils.SecurityUtils;
import com.sikaryofficial.system.api.model.UserPointResultVO;
import com.sikaryofficial.system.api.model.infomation.ArticleAssociateResp;
import com.sikaryofficial.system.api.model.infomation.ArticleDTO;
import com.sikaryofficial.system.api.model.infomation.ArticleListReq;
import com.sikaryofficial.system.api.model.infomation.ArticleTop10Req;
import com.sikaryofficial.system.api.model.infomation.KolListReq;
import com.sikaryofficial.system.api.model.infomation.StatuteListReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 文章Controller
 *
 * @author qinjinyuan
 * @date 2024-03-13
 */
@RestController
@RequestMapping("/article/frontend")
@Api(tags = "资讯-文章H5")
@Slf4j
@SuppressWarnings(value = "unchecked")
public class ArticleFrontEndController extends BaseController {
    @Autowired
    private ArticleFrontEndManager articleManager;
    @Autowired
    private DataRemoveManager dataRemoveManager;
    /**
     * 查询法律法规列表
     *
     */
    @PostMapping("/kolListPage")
    @ApiOperation("V3.1-kol文章帖子分页列表")
    @ReloadActionDataCache
    @Repeat(2)
    public PageResult<ArticleDTO> kolListPage(@Validated @RequestBody KolListReq req) {
        PageResult<ArticleDTO> page = articleManager.kolListPage(req);
        return PageUtil.success(page.getRows(), page.getTotal());
    }
    /**
     * 查询法律法规列表
     *
     */
    @PostMapping("/statuteListPage")
    @ApiOperation("V2.5-法律法规分页列表")
    @ReloadActionDataCache
    @Repeat(2)
    public PageResult<ArticleDTO> statuteListPage(@Validated @RequestBody StatuteListReq req) {
        PageResult<ArticleDTO> page = articleManager.statuteListPage(req);
        return PageUtil.success(page.getRows(), page.getTotal());
    }
    /**
     * 查询文章标题联想列表
     */
    @PostMapping("/associate")
    @ApiOperation("查询文章标题联想列表")
    @Repeat(2)
    public PageResult<ArticleAssociateResp> associateList(@Validated @RequestBody ArticleAssociateListReq req) {
        log.info("============>>> req:{}",req);
        IPage<ArticleAssociateResp> page = articleManager.associateList(req);
        return PageUtil.success(page.getRecords(), page.getTotal());
    }

    /**
     * 个人最新发布文章列表
     */
    @GetMapping("/owner/latest")
    @ApiOperation("个人最新发布文章列表")
    @Repeat
    public R<List<ArticleDTO>> ownerLatest() {
        Long userId = SecurityUtils.getUserId();
        if (Objects.isNull(userId) || Objects.equals(0L, userId)) {
            return R.ok(Collections.emptyList());
        }
        return R.ok(articleManager.ownerLatest());
    }

    /**
     * 查询文章列表
     */
    @PostMapping("/top10")
    @ApiOperation("文章推荐列表")
    @Deprecated
    @Repeat
    public PageResult<ArticleDTO> top10(@Validated @RequestBody ArticleTop10Req req) {
        return PageUtil.success(Lists.newArrayList(), 0);
    }

    /**
     * 查询文章列表
     */
    @PostMapping("/listPage")
    @ApiOperation("文章分页列表")
    @ReloadActionDataCache
    @Repeat(2)
    public PageResult<ArticleDTO> listPage(@Validated @RequestBody ArticleListReq req) {
        PageResult<ArticleDTO> page = articleManager.listPage(req);
        return PageUtil.success(page.getRows(), page.getTotal());
    }

    /**
     * 获取文章详细信息
     */
    @GetMapping(value = "/detail/{articleId}")
    @ApiOperation("获取文章详细信息")
    @Repeat
    public R<ArticleDTO> getInfo(@PathVariable("articleId") Long articleId) {
        return R.ok(articleManager.getInfo(articleId));
    }

    @GetMapping("/removeOwner/{articleIds}")
    @ApiOperation("删除文章")
    @Repeat
    public R<Boolean> removeOwner( @ApiParam(value = "articleIds 多个英文逗号分隔")
                                       @PathVariable String articleIds) {
        if (CharSequenceUtil.isBlank(articleIds)) {
            return R.fail("param is error");
        }
        List<Long> articleIdList = Lists.newArrayList();
        Arrays.stream(articleIds.split(",")).forEach(
                item-> articleIdList.add(Long.parseLong(item))
        );
        // 删除当前文章
        articleManager.removeOwner(articleIds);
        // 删除当前文章下面的 评论、回复及 相关所有用户行为数据：点赞，分享，评论
        dataRemoveManager.deleteArticleByObjId(articleIdList);
        return R.ok(true);
    }

    /**
     * 新增文章
     */
    @PostMapping(value = "/save")
    @ApiOperation("新增或保存文章")
    @Repeat
    public R<UserPointResultVO> saveArticle(@Validated @RequestBody ArticleReq req) {
        return R.ok(articleManager.saveArticle(req));
    }

    /**
     * 后台管理-活动类型
     */
    @PostMapping("/getContentType")
    @ApiOperation("文章内容类型")
    public R<List<SysDictDataDTO>> getContentType() {
        ArticleContentTypeEnum[] reportTypeEnums = ArticleContentTypeEnum.values();
        List<SysDictDataDTO> result = new ArrayList<>();
        for (ArticleContentTypeEnum reportTypeEnum : reportTypeEnums) {
            SysDictDataDTO sysDictDataDTO = new SysDictDataDTO();
            result.add(sysDictDataDTO);
            sysDictDataDTO.setDictDesc(reportTypeEnum.getName());
            sysDictDataDTO.setDictValue(reportTypeEnum.getCode() + "");
            sysDictDataDTO.setDictType("content_type");
        }
        return R.ok(result);
    }

}
